//
// Created by 86183 on 2022/12/6.
//

#include "lab52.h"

extern int CurrentCnt;

GoodsList* read_txt(FILE* fp);

void init_list(GoodsList **pL)
{
    *pL = (GoodsList*) malloc(sizeof(GoodsList));//头节点空间
    (*pL) -> next = NULL;// Head 指向的 next(第一个节点)置空

    FILE* fp;//读取文件
    fp = fopen("C:\\Users\\86183\\Desktop\\c_test\\iCoding_6\\goodsinfo.txt", "r");

    GoodsList* good = NULL;
    GoodsList* tmp = NULL;
    int count = 0;
    while ((good = read_txt(fp)) != NULL)
    {
        if (count == 0)
        {
            good -> next = (*pL) -> next;
            (*pL) -> next = good;
            tmp = good;
        }
        else
        {
            good -> next = tmp -> next;
            tmp -> next = good;
            tmp = good;
        }
        count ++;
    }
    CurrentCnt = count;
    printf("读取完成\n");
}

GoodsList* read_txt(FILE* fp)
{
    GoodsList* good = (GoodsList*) malloc(sizeof(GoodsList));
    assert(good != NULL);//验证 malloc 申请是否成功
    int ret = fscanf(fp, "%s %s %d %s %d %d\n",
           good -> data.goods_id,
           good -> data.goods_name,
           &good -> data.goods_price,
           good -> data.goods_discount,
           &good -> data.goods_amount,
           &good -> data.goods_remain);

    if (ret == EOF)
    {
        free(good);//释放空间
        good = NULL;
        fclose(fp);
    }
    return good;
}

void output_one_item(GoodsList *L)
{
    printf("\n");
    printf("%s\t", L -> data.goods_id);
    printf("%s\t", L -> data.goods_name);
    printf("%d\t", L -> data.goods_price);
    printf("%s  \t", L -> data.goods_discount);
    printf("%d\t", L -> data.goods_amount);
    printf("%d\t", L -> data.goods_remain);
}

bool change_item(GoodsList *L, char* goods_id, GoodsInfo new_info)
{
    printf("修改某个商品的信息\n");
    GoodsList* good = search_item(L, goods_id);
    if (good == NULL)
    {
        printf("修改失败，没有找到此商品\n");
        return false;
    }
    else
    {
        good -> data = new_info;
        printf("\n已经将 id 为 %s 的商品修改为:\n", goods_id);
        search_item(L, new_info.goods_id);
        return true;
    }
}

bool insert_item(GoodsList *L, GoodsInfo item, int choice)
{
    printf("插入某个商品的信息\n");
    switch (choice)
    {
        case 1:
        {
            GoodsList* good = (GoodsList*) malloc(sizeof(GoodsList));
            good -> data = item;

            good -> next = L -> next;
            L -> next = good;
            CurrentCnt++;
            return true;
        }
        case 0:
        {
            GoodsList* good = (GoodsList*) malloc(sizeof(GoodsList));
            good -> data = item;

            while (L -> next != NULL)
            {
                L = L -> next;
            }
            good -> next = L -> next;
            L -> next = good;
            CurrentCnt++;
            return true;
        }
        default:
        {
            GoodsList* good = (GoodsList*) malloc(sizeof(GoodsList));
            good -> data = item;

            int i;
            for (i = 0; i < choice - 1; i++)
            {
                L = L -> next;
                if (L == NULL)
                {
                    return false;
                }
            }
            good -> next = L -> next;
            L -> next = good;
            CurrentCnt++;
            return true;
        }
    }
}

bool delete_item(GoodsList *L, char* goods_id)
{
    printf("删除某个商品的信息\n");
    /* 注意先判断链表中有没有文件 */
    if (L -> next == NULL)
    {
        printf("删除失败，链表中没有商品\n");
        return false;
    }
    while (strcmp(goods_id, L -> next -> data.goods_id) != 0)
    {
        L = L -> next;
        if (L -> next == NULL)
        {
            printf("删除失败\n");
            return false;
        }
    }

    GoodsList* tmp = L -> next;
    L -> next = L -> next -> next;
    free(tmp);
    tmp = NULL;
    CurrentCnt--;
    printf("删除成功\n");
    return true;
}

void output_all_items(GoodsList *L)
{
    printf("显示所有商品的信息\n");
    if (L -> next == NULL)
    {
        printf("链表中没有商品\n");
    }
    while (L -> next != NULL)
    {
        L = L -> next;
        output_one_item(L);
    }
}

int save_to_file(GoodsList *L)
{
    printf("商品存盘并退出系统\n");

    FILE* fp;
    fp = fopen("C:\\Users\\86183\\Desktop\\c_test\\iCoding_6\\goodsinfo.txt", "w");

    if (L == NULL || L -> next == NULL)
    {
        return 0;
    }

    while (L -> next != NULL)
    {
        fprintf(fp, "%s", L -> next -> data.goods_id);
        fprintf(fp, "\t");
        fprintf(fp, "%s", L -> next -> data.goods_name);
        fprintf(fp, "\t\t");
        fprintf(fp, "%d", L -> next -> data.goods_price);
        fprintf(fp, "\t");
        fprintf(fp, "%s", L -> next -> data.goods_discount);
        fprintf(fp, "  \t");
        fprintf(fp, "%d", L -> next -> data.goods_amount);
        fprintf(fp, "\t");
        fprintf(fp, "%d", L -> next -> data.goods_remain);
        fprintf(fp, "\n");
        L = L -> next;
    }

    fclose(fp);
    return CurrentCnt;
}

void bubble_sort(GoodsList *L)
{
    printf("对商品价格进行排序\n");
    if (CurrentCnt == 1 || !CurrentCnt)
        return;
    int i;
    for (i = 0; i < CurrentCnt; i++) // i 趟冒泡排序
    {
        for (GoodsList* pre = L; pre -> next -> next != NULL; pre = pre -> next)
        {
            GoodsList* now = pre -> next;
            GoodsList* cur = now -> next;
            if (now -> data.goods_price > cur -> data.goods_price)
            {
                pre -> next = cur;
                now -> next = cur -> next;
                cur -> next = now;
            }
        }
    }
}

void destory_list(GoodsList **pL)
{
    if ((*pL) == NULL)
    {
        printf("链表已销毁，请勿重复销毁\n");
    }
    else
    {
        while ((*pL) -> next != NULL)
        {
            GoodsList* tmp = (*pL) -> next;
            (*pL) -> next = tmp -> next;
            free(tmp);
            tmp = NULL;
        }
        free(*pL); /* 释放头节点 */
        *pL = NULL;
        CurrentCnt = 0;
        printf("销毁成功\n");
    }
}

void destory_list_and_file(GoodsList **pL)
{
    printf("(慎用)删除所有内容\n");
    destory_list(pL);

    remove("C:\\Users\\86183\\Desktop\\c_test\\iCoding_6\\goodsinfo.txt");
}

GoodsList* search_item(GoodsList *L, char* goods_id)
{
    if (L -> next == NULL)
    {
        printf("无法查询，链表中没有文件\n");
        return NULL;
    }
    while (L -> next != NULL)
    {
        L = L -> next;
        if (!strcmp(L -> data.goods_id, goods_id))
        {
            printf("查询成功\n");
            output_one_item(L);
            return L;
        }
    }
    printf("查询失败，未能找到此商品\n");
    return false;
}

GoodsInfo read_goods_info()
{
    GoodsInfo* good = (GoodsInfo*) malloc(sizeof(GoodsInfo));
    printf("\n请输入插入的商品 id，名称，价格，折扣，数量和剩余数量\n");
    printf("请输入商品 id:>");
    scanf("%s", good -> goods_id);
    printf("请输入商品名称:>");
    scanf("%s", good -> goods_name);
    printf("请输入商品价格:>");
    scanf("%d", &good -> goods_price);
    printf("请输入商品折扣:>");
    scanf("%s", good -> goods_discount);
    printf("请输入商品数量:>");
    scanf("%d", &good -> goods_amount);
    printf("请输入商品剩余数量:>");
    scanf("%d", &good -> goods_remain);

    return *good;
}